from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.conf import settings


# Create your models here.

class CommentManage(models.Manager):
    def check_content(self, content):
        pass

    def comment(self, content, user, type):
        instance = self.model(
            type=type,
            user=user,
            content=content,
        )
        instance.save()
        # self.


class Comment(models.Model):
    # ARTICLE = 0
    # DIARY = 1
    # COMMENT = 2
    # TYPE_CHOICES = (
    #     (ARTICLE, '文章'),
    #     (DIARY, '日志'),
    #     (COMMENT, '评论'),
    #
    # )

    content_type = models.ForeignKey(ContentType, on_delete=models.PROTECT)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey(
        ct_field="content_type",
        fk_field="object_id"
    )

    # type = models.PositiveSmallIntegerField(verbose_name='主体类型', choices=TYPE_CHOICES)
    member = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='评论人', on_delete=models.PROTECT)
    content = models.TextField(verbose_name='内容')
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    delete = models.BooleanField(verbose_name='删除', default=False, editable=False)
    like_count = models.IntegerField(verbose_name='点赞', default=0)
    parent = models.ForeignKey('Comment', verbose_name='回复评论', on_delete=models.PROTECT, null=True)

    @staticmethod
    def get_content_type():
        return ContentType.objects.get(model='Comment'.lower())

    class Meta:
        db_table = 'comment'
        verbose_name = '评论'
